<?php

namespace App\Controller\Home;

use App\Controller\Example\ShopGoodsOrderExample;
use App\Controller\Example\UserExample;
use App\Models\Conn;
use App\Models\ShopGoodsModel;
use App\Models\ShopGoodsOrderModel;
use App\Models\ShopGoodsSkuModel;
use App\Models\UsersModel;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;

class Order
{
    /**
     * TODO 订单列表
     * entry  token order_state page limit
     * return json
     * */
    public function orderList($data){
        $assemble = Common::assemble(new ShopGoodsOrderExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $assemble->member_id = UsersModel::getUserInside($conn,$assemble->token)["id"];
        if($assemble->order_state == 0 || $assemble->order_state == null){
            $assemble->order_state = "1,2,3,4,5";
        }
        $goodsOrderList = ShopGoodsOrderModel::getGoodsOrderList($conn, $assemble, $identical);
        foreach ($goodsOrderList as $item => $value){
            $goodsSkuInfo = ShopGoodsSkuModel::getGoodsSkuInfo($conn, $value["sku_id"]);
            $goodsOrderList[$item]["sku"] = $goodsSkuInfo;
            $jj_file = $conn->readConn("select `file_id`,`storage`,`domain`,`file_path` from `jj_file` where `file_id` =({$goodsSkuInfo['sku_img_id']})");
            $goodsOrderList[$item]['file_url'] = Common::fileUrl($jj_file);
            $goodsOrderList[$item]["goods"] = ShopGoodsModel::getGoodsInfo($conn,$value["goods_id"]);
        }
        $list = $goodsOrderList;
        Result::success($list);
    }
    /**
     * TODO 订单详情
     * entry  token order_id
     * return json
     * */
    public function orderDetails($data){
        $assemble = Common::assemble(new ShopGoodsOrderExample(), $data);
        $conn = new Conn();
        $assemble->member_id = UsersModel::getUserInside($conn,$assemble->token)["id"];
        $goodsOrderList = ShopGoodsOrderModel::getGoodsOrder($conn, $assemble);
        $goodsSkuInfo = ShopGoodsSkuModel::getGoodsSkuInfo($conn, $goodsOrderList["sku_id"]);
        $goodsOrderList["sku"] = $goodsSkuInfo;
        $jj_file = $conn->readConn("select `file_id`,`storage`,`domain`,`file_path` from `jj_file` where `file_id` =({$goodsSkuInfo['sku_img_id']})");
        $goodsOrderList['file_url'] = Common::fileUrl($jj_file);
        $goodsOrderList["goods"] = ShopGoodsModel::getGoodsInfo($conn,$goodsOrderList["goods_id"]);
        Result::success($goodsOrderList);
    }
    /**
     *TODO 删除订单
     * entry  $data order_id
     * return json
     * */
    public function delOrder($data){
        $assemble = Common::assemble(new ShopGoodsOrderExample(), $data);
        $conn = new Conn();
        $assemble->member_id = UsersModel::getUserInside($conn, $assemble->token)["id"];
        $goodsOrderInfo = ShopGoodsOrderModel::getGoodsOrderInfo($conn, $assemble);
        if(!$goodsOrderInfo){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        if($goodsOrderInfo["is_delete"] == 1){
            Result::error(ErrorEnum::DELETESUCCESSFUL[$assemble->language]);
        }
        $conn->shiwuStart();
        $assemble->is_delete = 1;
        if(ShopGoodsOrderModel::upShopGoodsOrderInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::DELETEFAILED[$assemble->language]);
        }
        $conn->shiwuEnd();
        Result::success(ErrorEnum::DELETESUCCESSFUL[$assemble->language]);
    }
    /**
     *TODO 确认收货
     * entry  token order_id
     * return json
     * */
    public function confirmReceipt($data){
        $assemble = Common::assemble(new ShopGoodsOrderExample(), $data);
        $conn = new Conn();
        $UserExample = Common::assemble(new UserExample(),UsersModel::getUserInside($conn, $assemble->token));
        $assemble->member_id = $UserExample->id;
        $goodsOrderInfo = ShopGoodsOrderModel::getGoodsOrderInfo($conn, $assemble);
        if(!$goodsOrderInfo){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        if($goodsOrderInfo["order_state"] != 2){
            Result::error(ErrorEnum::NONACTIONABLEORDERS[$assemble->language]);
        }
        $conn->shiwuStart();
        $assemble->order_state = 3;
        if(ShopGoodsOrderModel::upShopGoodsOrderInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::OPERATIONFAILED[$assemble->language]);
        }
        $conn->shiwuEnd();
        Result::success(ErrorEnum::SETTINGSUCCEEDED[$assemble->language]);
    }
}